package com.xy.hms.controller;

import com.xy.hms.model.Room;
import com.xy.hms.model.RoomOrder;
import com.xy.hms.service.OrderServiceI;
import com.xy.hms.service.RoomServiceI;
import com.xy.hms.util.DataUtil;
import com.xy.hms.util.RedisCache;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author xiaye
 * @create 2020-04-29-9:19
 */
@Api(tags = "订单控制")
@Controller
@Slf4j
public class OrderController {
    @Autowired
    OrderServiceI orderServiceI;

    @Autowired
    RoomServiceI roomServiceI;

    @GetMapping("/order/orders")
    public String selectAllOrders(Model model){
        List<RoomOrder> roomOrders = orderServiceI.selectAllOrders();
        model.addAttribute("orders",roomOrders);
        return "booking/view_booking";
    }

    @PostMapping("/order/addOrder")
    public String addOrder(RoomOrder order){

        Room room = roomServiceI.selectRoomByid(order.getId());
        order.setPayment("未支付");
        log.error(order.toString());
        int i = orderServiceI.addOrder(order);
        return "redirect:/order/orders";
    }

    @GetMapping("/order/toaddOrder")
    public String  toAddOrderPage(Model model){
        List<Room> rooms = roomServiceI.selectAllRooms();
        model.addAttribute("rooms",rooms);
        return "booking/new_booking";
    }


    @GetMapping("/order/{id}")
    @ResponseBody
    public DataUtil  toUpdatePage(@PathVariable("id")Integer id,DataUtil dataUtil){
        RoomOrder roomOrder = orderServiceI.selectOrderById(id);
//        log.error(roomOrder.toString());
        List<RoomOrder> roomOrders = Arrays.asList(roomOrder);
        dataUtil.setData(roomOrders);
        dataUtil.setResult(true);
        dataUtil.setMsg("回显订单信息成功");
        return dataUtil;
    }

    @PutMapping("/order")
    @ResponseBody
    public DataUtil updateOrder(RoomOrder roomOrder,DataUtil dataUtil){
        log.error(roomOrder.toString());
        int i = orderServiceI.updateOrder(roomOrder);
        if (i!=0){
            dataUtil.setMsg("修改成功！");
            dataUtil.setResult(true);
        }else {
            dataUtil.setMsg("修改失败！");
            dataUtil.setResult(false);
        }
        return dataUtil;
    }



}
